package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mapper.LanguageMapper;
import com.mapper.LanguageVersionMapper;
import com.model.LanguageDO;
import com.model.LanguageVersionDO;
import com.service.LanguageService;
import com.utils.JsonData;
import com.vo.LanguageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author rkc
 * @date 2022/8/25 16:59
 */
@Service
public class LanguageServiceImpl implements LanguageService {

    @Resource
    private LanguageMapper languageMapper;

    @Resource
    private LanguageVersionMapper languageVersionMapper;

    /**
     * 查询全部语言及其相关信息
     * @return
     */
    @Override
    public JsonData selectAll() {
        //查询出所有语言相关
        List<LanguageDO> languageDOList = languageMapper.selectList(null);
        List<LanguageVO> languageVOList = languageDOList.stream().map(obj -> {
            LanguageVO languageVO = new LanguageVO();
            BeanUtils.copyProperties(obj, languageVO);
            return languageVO;
        }).collect(Collectors.toList());
        for (LanguageVO languageVO : languageVOList) {
            //查询对应的版本
            languageVO.setLanguageVersions(languageVersionMapper.selectList(new QueryWrapper<LanguageVersionDO>()
                    .eq("lid", languageVO.getId())));
        }
        return JsonData.buildSuccess(languageVOList);
    }
}
